<?php

class rvg extends module {

	public $title = "Radiovazogasy Module";
	public $author = "Hery";
	public $version = "0.9";
	public $host = "radiovazogasy.com";
	private $wsid = "karajia";
	public $client;

	
	public function init()
	{
		$this->vonjy_ini = new ini("data/vonjy.ini");
		$this->vonjy_ini->setIniVal("radiovazogasy", "!rvg", "Mampiseho ny baiko ilaina amin'ny Radiovazogasy");
		$this->vonjy_ini->writeIni();
		require_once("libraries/xml.php");
	}
	public function priv($line, $args)
	{

		if ($args['nargs'] == 0)
		{
			$arg = "help";
		}
		else
		{
			$arg = $args['arg1'];
		}
		switch ($arg)
		{
			case "help":
				$this->ircClass->privMsg($line['fromNick'], "Tonga soa eto amin'ny " . GREEN . "Radio" . BLUE . "Vazo" . RED . "Gasy" . NOFORMAT . ". ireto avy ny baiko azo ampiasaina");
				$this->ircClass->privMsg($line['fromNick'], RED ."!rvg mandeha : " . NOFORMAT . "Mampiseho ny hira mandeha amin'izao fotoana izao");
				$this->ircClass->privMsg($line['fromNick'], RED ."!rvg miandry : " . NOFORMAT . "Mampiseho ny hira handeha efa milahatra ao anaty lisitra");
				$this->ircClass->privMsg($line['fromNick'], RED ."!rvg tadiavo " . BLUE . "<lohateny na mpihira> : " . NOFORMAT . "Mitady ny hira na mpihira". GREEN . " ohatra: " . NOFORMAT . "!rvg tadiavo Rija oelijaona");
				$this->ircClass->privMsg($line['fromNick'], RED ."!rvg alaharo " . BLUE . "<laharana> : " . NOFORMAT . "Mandahatra ny hira voalazan'ny <laharana>". GREEN . " ohatra: " . NOFORMAT . "!rvg alaharo 654");
				$this->ircClass->privMsg($line['fromNick'], RED ."!rvg hafaro " . BLUE . "<laharana> <hafatra>" . NOFORMAT . "Mametraka ny hafatra <hafatra> amin'ny hira voalazan'ny <laharana>" . GREEN . " ohatra: " . NOFORMAT . "!rvg hafaro 65224 Misaotra anao");

			
			break;
			case "search":
			case "rechercher":
			case "tadiavo":

				if(!isset($args['arg2'])) {
					$this->ircClass->privMsg($line['fromNick'], "Tsy nametraka lohateny na mpihira hotadiavina ianao. Soraty ny baiko !rvg tadiavo <teny tadiavina>");
					Return ;
				} else {
					$tadiavo = trim(substr($args['query'], (strpos($args['query'], $args['arg1']) + (strlen($args['arg1'])))));
					
				    $query = "tadiavo=" . urlencode($tadiavo);
					$request = "http://radiovazogasy.com/radio/rest/tadiavo";
					$session = curl_init($request);
				    // Tell curl to use HTTP POST
				    curl_setopt ($session, CURLOPT_POST, true); 
				    // Tell curl that this is the body of the POST
				    curl_setopt ($session, CURLOPT_POSTFIELDS, $query); 
				    // Tell curl not to return headers, but do return the response
				    curl_setopt($session, CURLOPT_HEADER, false); 
				    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
					$response = curl_exec($session); 
					curl_close($session);
					$xml = xmlize($response);
					if (isset($xml['rvg']['#']['error']))
					{
						$msg = $xml['rvg']['#']['error']['0']['#'];
						$this->ircClass->privMsg($line['fromNick'], html_entity_decode($msg));
					
					}
					else
					{
						$this->ircClass->privMsg($line['fromNick'], "Ireto ny hira hita. Izay misy isa dia azonao asiana alahatra amin'ny baiko !rvg alaharo");
						foreach ($xml['rvg']['#']['song'] as $song)
						{
							$msg = $song['#']['artist']['0']['#'] . ", " . $song['#']['title']['0']['#'] . "(" . $song['#']['duration']['0']['#'] . ") , " . RED . "!rvg alaharo " . $song['#']['id']['0']['#'] ; 
							$this->ircClass->privMsg($line['fromNick'], html_entity_decode($msg));
						}
					}
					return;
				}
			break;
			case "alaharo":
				if(!$user = $this->getModule("user")->loggedin($line))
				{
					$this->getModule("user")->printError($line);
					return;
				}
				
				if(!isset($args['arg2'])) {
					$this->ircClass->privMsg($line['fromNick'], "Tsy nametraka ny laharan'ny hira halahatra ianao. Soraty " . RED . "!rvg tadiavo <hira>" . NOFORMAT . " raha hitady hira");
					Return ;
				} elseif(intval($args['arg2']) == 0) {
					$this->ircClass->privMsg($line['fromNick'], "Tsy laharana ara-dalana io napetrakao io.");
					Return ;
				} else {
				
					
				    $query = "username=" . $user['username'] . "&password=" . $user['password'] . "&songID=" . $args['arg2'];
					$request = "http://radiovazogasy.com/radio/rest/alaharo";
					$session = curl_init($request);
				    // Tell curl to use HTTP POST
				    curl_setopt ($session, CURLOPT_POST, true); 
				    // Tell curl that this is the body of the POST
				    curl_setopt ($session, CURLOPT_POSTFIELDS, $query); 
				    // Tell curl not to return headers, but do return the response
				    curl_setopt($session, CURLOPT_HEADER, false); 
				    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
					$response = curl_exec($session); 
					
					var_dump($response);
					curl_close($session);
					$xml = xmlize($response);
					if (isset($xml['rvg']['#']['error']))
					{
						$msg = $xml['rvg']['#']['error']['0']['#'];
						$this->ircClass->privMsg($line['fromNick'], html_entity_decode($msg));
					
					}
					else
					{
						$msg = $xml['rvg']['#']['result']['0']['#'];
						$this->ircClass->privMsg($line['fromNick'], html_entity_decode($msg));
						
						$msg = "Raha hametraka hafatra amin'iny hira iny ianao dia soraty hoe" . RED . "!rvg hafaro " . $args['arg2'] . " hafatra";
						$this->ircClass->privMsg($line['fromNick'], $msg);
						
					}
					Return ;
				}
			
			break;
			case "hafaro":

				if(!$user = $this->getModule("user")->loggedin($line))
				{
					$this->getModule("user")->printError($line);
					return;
				}

				if(!isset($args['arg3'])) {
					$this->ircClass->privMsg($line['fromNick'], "Diso ny baiko napetrakao. Ny baiko marina dia " . BOLD . "!rvg hafaro <laharana> <hafatra>" . BOLD . " (ka ny laharana dia ilay laharan'ny hira ao amin'ny !rvg miandry");
					Return ;
				} elseif(intval($args['arg2']) == 0) {
					$this->ircClass->privMsg($line['fromNick'], "Diso ny baiko napetrakao. Ny baiko marina dia " . BOLD . "!rvg hafaro <laharana> <hafatra>" . BOLD . " (ka ny laharana dia ilay laharan'ny hira ao amin'ny !rvg miandry");
					Return ;
				} else {
					$msg = trim(substr($args['query'], (strpos($args['query'], $args['arg2']) + (strlen($args['arg2'])))));
					
					
					
				    $query = "username=" . $user['username'] . "&password=" . $user['password'] . "&songID=" . $args['arg2'] . "&message=" . urlencode($msg);
					
					$request = "http://radiovazogasy.com/radio/rest/hafatra";
					$session = curl_init($request);
				    // Tell curl to use HTTP POST
				    curl_setopt ($session, CURLOPT_POST, true); 
				    // Tell curl that this is the body of the POST
				    curl_setopt ($session, CURLOPT_POSTFIELDS, $query); 
				    // Tell curl not to return headers, but do return the response
				    curl_setopt($session, CURLOPT_HEADER, false); 
				    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
					$response = curl_exec($session); 
					curl_close($session);
					
					$xml = xmlize($response);
					if (isset($xml['rvg']['#']['error']))
					{
						$msg = $xml['rvg']['#']['error']['0']['#'];
						$this->ircClass->privMsg($line['fromNick'], html_entity_decode($msg));
					
					}
					else
					{
						$msg = $xml['rvg']['#']['result']['0']['#'];
						$this->ircClass->privMsg($line['fromNick'], html_entity_decode($msg));
						
					}
					Return ;
				}
			break;
			case "playing":
			case "mandeha":
				$request = "http://radiovazogasy.com/radio/rest/mandeha";
				$session = curl_init($request);
				curl_setopt($session, CURLOPT_HEADER, false); 
				curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
				$response = curl_exec($session); 
				curl_close($session);
				
				$xml = xmlize($response);
				if (isset($xml['rvg']['#']['error']))
				{
					$msg = $xml['rvg']['#']['error']['0']['#'];
					$this->ircClass->privMsg($line['fromNick'], html_entity_decode($msg));
				
				}
				else
				{
					$this->ircClass->privMsg($line['fromNick'], "Hira: " . $xml['rvg']['#']['playing']['0']['#']['artist']['0']['#'] . " - " . $xml['rvg']['#']['playing']['0']['#']['title']['0']['#']);
					$this->ircClass->privMsg($line['fromNick'], "Raki-kira: " . $xml['rvg']['#']['playing']['0']['#']['album']['0']['#']);
					$this->ircClass->privMsg($line['fromNick'], "Nandahatra: " . $xml['rvg']['#']['playing']['0']['#']['requester']['0']['#']);
					$this->ircClass->privMsg($line['fromNick'], "Mpihaino: " . $xml['rvg']['#']['playing']['0']['#']['listeners']['0']['#']);
					$this->ircClass->privMsg($line['fromNick'], "Hafatra: " . $xml['rvg']['#']['playing']['0']['#']['message']['0']['#']);
					$this->ircClass->privMsg($line['fromNick'], "Nanafatra: " . $xml['rvg']['#']['playing']['0']['#']['username']['0']['#']);
				}
				return;
			break;
			case "miandry":
			case "queue":
				$request = "http://radiovazogasy.com/radio/rest/miandry/5";
				$session = curl_init($request);
				curl_setopt($session, CURLOPT_HEADER, false); 
				curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
				$response = curl_exec($session); 
				curl_close($session);

				$xml = xmlize($response);
				if (isset($xml['rvg']['#']['error']))
				{
					$msg = $xml['rvg']['#']['error']['0']['#'];
					$this->ircClass->privMsg($line['fromNick'], html_entity_decode($msg));
				
				}
				else
				{
					$this->ircClass->privMsg($line['fromNick'], "Ireto ny hira handeha rehefa avy eo. Azonao asiana hafatra amin'ny baiko " . RED . "!rvg hafaro <isa>");
					foreach ($xml['rvg']['#']['queue'] as $val)
					{
						$msg = $val['#']['artist']['0']['#'] . ", " . $val['#']['title']['0']['#'] . "(" . $val['#']['duration']['0']['#'] . ") , " . RED . "!rvg hafaro " . $val['#']['id']['0']['#'] ; 
						$this->ircClass->privMsg($line['fromNick'], html_entity_decode($msg));
					}
				}

				return;
			break;
		}
	}
	
	
	

}

?>
